package com.face.server.communication.mina.coder;

import com.face.server.common.domain.ByteArrayReceiverEx;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/**
 * @author yangzhongming
 * @desc 人脸识别出纸机--mina 编码器
 * @date 2022/1/24
 */
public class ByteArrayEncoder extends ProtocolEncoderAdapter {
    @Override
    public void encode(IoSession ioSession, Object message, ProtocolEncoderOutput out) throws Exception {
        ByteArrayReceiverEx byteArrayReceiverEx = (ByteArrayReceiverEx) message;

        byte[] bytes = byteArrayReceiverEx.toArray();
        IoBuffer buffer = IoBuffer.allocate(bytes.length);
        buffer.put(bytes);
        buffer.flip();
        out.write(buffer);
        out.flush();
        buffer.free();
    }
}
